package team.knowers.qzhserver.user.userFollower.service.impl;

import com.alibaba.druid.sql.ast.expr.SQLExprUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.knowers.qzhserver.common.entity.BaseDO;
import team.knowers.qzhserver.user.userData.entity.dao.UserDataDO;
import team.knowers.qzhserver.user.userData.mapper.UserDataMapper;
import team.knowers.qzhserver.user.userData.service.UserDataService;
import team.knowers.qzhserver.user.userFollower.entity.dao.UserFollowerDO;
import team.knowers.qzhserver.user.userFollower.mapper.UserFollowerMapper;
import team.knowers.qzhserver.user.userFollower.service.UserFollowerService;

import java.util.Date;
import java.util.List;

/**
 * @author zcb
 * @date 2024/3/5 19:59
 */
@Service
public class UserFollowerImpl implements UserFollowerService {
    @Autowired
    private UserFollowerMapper userFollowerMapper;
    @Autowired
    private UserDataMapper userDataMapper;
    @Override
    public Boolean follow(String fid, String uid) {
        UserFollowerDO userFollowerDO = new UserFollowerDO(fid,uid);
        List<UserFollowerDO> userFollowerDOList = userFollowerMapper.selectList(new QueryWrapper<UserFollowerDO>().eq("uid",uid).eq("fid",fid));
        if(!userFollowerDOList.isEmpty()){
            return false;
        }
        if(increaseFanCount(uid)!=1)
            return false;
        if(increaseFollowCount(fid)!=1)
            return false;
        userFollowerMapper.insert(userFollowerDO);
        return true;
    }

    @Override
    public Boolean unfollow(String fid, String uid) {
        List<UserFollowerDO> userFollowerDOList = userFollowerMapper.selectList(new QueryWrapper<UserFollowerDO>().eq("uid",uid).eq("fid",fid));
        if(userFollowerDOList.isEmpty()){
            return false;
        }
        if(decreaseFanCount(uid)!=1)
            return false;
        if(decreaseFollowCount(fid)!=1)
            return false;
        userFollowerMapper.deleteById(userFollowerDOList.get(0).getId());
        return true;
    }

    private int increaseFanCount(String id) {
        UpdateWrapper<UserDataDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("gmt_modified",new Date());
        updateWrapper.setSql("fan_count = fan_count + 1");
        return userDataMapper.update(null,updateWrapper);
    }
    private int decreaseFanCount(String id) {
        UpdateWrapper<UserDataDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("gmt_modified",new Date());
        updateWrapper.setSql("fan_count = fan_count - 1");
        return userDataMapper.update(null,updateWrapper);
    }
    private int increaseFollowCount(String id) {
        UpdateWrapper<UserDataDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("gmt_modified",new Date());
        updateWrapper.setSql("follow_count = follow_count + 1");
        return userDataMapper.update(null,updateWrapper);
    }
    private int decreaseFollowCount(String id) {
        UpdateWrapper<UserDataDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("gmt_modified",new Date());
        updateWrapper.setSql("follow_count = follow_count - 1");
        return userDataMapper.update(null,updateWrapper);
    }
}
